//
//  XOWMHelper.m
//  XOWMSDK
//
//  Created by skyline on 2018/7/25.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "XOWMHelper.h"
#import "TCCIAPModel.h"

@interface XOWMHelper()
{
    NSMutableDictionary *_localInfo;
    NSMutableArray *_accountArr;
    NSString *_account;
    BOOL _isBind;
    BOOL _isAuthenticate;
}
@end

@implementation XOWMHelper
static XOWMHelper*_helper;
static dispatch_once_t onceToken;
static XOWMHelperPara _para;
XOWMHelperManager helperManager()
{
    XOWMHelperManager manager =
    {
        _getPara,
        _setPara,
        _insertAccount,
        _setupBindState,
        _setupAuthenticate
    };
    return manager;
}

static XOWMHelperPara _getPara()
{
    if ([_para.appID length] < 1)
    {
        _para.isBind = NO;
        _para.isAuthenticate = NO;
        _para.country = [CCIP IP].wanCountry;
        _para.city = [CCIP IP].wanCity;
        _para.area = [CCIP IP].wanRegion;
        _para.style = XOWMPlatformStyleDefault;
        _para.layout = XOWMPlatformLayoutDefault;
        _para.color = THEME_COLOR;
    }
    _para.localInfo = _localInfo();
    _para.accountArr = _accountArr();
    _para.country = [CCIP IP].wanCountry;
    _para.city = [CCIP IP].wanCity;
    _para.area = [CCIP IP].wanRegion;
    _para.currentVersion = ([[NSBundle mainBundle].infoDictionary valueForKey:@"currentversion"])?[[NSBundle mainBundle].infoDictionary valueForKey:@"currentversion"]:@"1.1.9";
    return _para;
}
static void _setPara(XOWMHelperPara para)
{
    _para = para;
}
static void _insertAccount(NSDictionary *dict)
{
    _para.account = [dict valueForKey:@"name"];
    NSString *password = [dict valueForKey:@"password"];
    if (_para.account && password)
        [_para.accountArr yjd_enumerate:@{_para.account:password}];
}
static void _setupBindState(NSString *string)
{
    _para.isBind = ([string length] > 1)?YES:NO;
}
static void _setupAuthenticate(NSString *string)
{
    _para.isAuthenticate = ([string intValue] == 1)?YES:NO;
}
static NSDictionary * _localInfo()
{
    if ([_para.appID length] > 1)
    {
        NSDictionary * localInfo = [NSMutableDictionary dictionary];
        [localInfo setValue:[CCDevice UUID] forKey:@"imei"];//设备号<设备唯一标识符>
        [localInfo setValue:[CCIP IP].wanIP forKey:@"ip"];//设备公网IP//获取公网IP比较费时，做异步处理
        [localInfo setValue:_para.appID forKey:@"appid"];//appid<暂定1>
        [localInfo setValue:@"1"forKey:@"terminal"];//设备平台<苹果设备为1>
        [localInfo setValue:@"" forKey:@"phone"];//添加手机字段
        return localInfo;
    }
    else return nil;
}
static NSMutableArray *_accountArr()
{
    return  [[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:SKLTokenKey];
    //        _accountArr = [NSMutableArray arrayWithArray:[CCCache valueFromKey:SKLTokenKey]];//直接操作accountArr增删动作，自动存储，不需要再调用存储操作
}
@end
